package com.sg.service.biz.rbff.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.rbff.req.*;
import com.sg.dto.biz.rbff.res.*;
import com.sg.service.biz.rbff.ResourceFileService;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class ResourceFileServiceImpl implements ResourceFileService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    com.sg.service.biz.dataassets.AlbumService fwDataassetsAlbumClient;

    @Resource
    com.sg.service.biz.dataassets.UnstructuredResourcesService fwDataassetsUnstructuredResourcesClient;
    @Resource
    com.sg.service.biz.dataassets.ResourceFileService fwDataassetsResourceFileClient;
    @Resource
    com.sg.service.biz.dataassets.IntelligentCommunicationService fwDataassetsIntelligentCommunicationClient;

    /**
     * B8存档专辑下文件[10356]
     * gen by moon at 11/18/2024, 10:14:48 AM
     */
    @Trace(operationName = "B8存档专辑下文件")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffUpdateAlbumFileArchiveRespDto updateAlbumFileArchive(BffUpdateAlbumFileArchiveReqDto reqDto) {


        //步骤0: D8查询专辑文件关系详情(公共) - queryAlbumFileRelationDetailCom
        QueryAlbumFileRelationDetailComRespDto queryAlbumFileRelationDetailComRespDto = null;
        QueryAlbumFileRelationDetailComReqDto queryAlbumFileRelationDetailComReqDto = new QueryAlbumFileRelationDetailComReqDto();
        if (reqDto != null) {
            queryAlbumFileRelationDetailComReqDto.setAlbumRelationId(reqDto.getAlbumRelationId());//SimpleFieldAssign//sourceId:2028504_1
        }

        /*D8查询专辑文件关系详情(公共)[10342]   */
        Assert.isNull(queryAlbumFileRelationDetailComReqDto.getAlbumRelationId(), "B8存档专辑下文件-D8查询专辑文件关系详情(公共)-专辑文档关系ID不能为空", false);
        queryAlbumFileRelationDetailComRespDto = fwDataassetsAlbumClient.queryAlbumFileRelationDetailCom(queryAlbumFileRelationDetailComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


//步骤1: D8执行生命周期判断(公共) - implementSubjectLifeCycleJudgeCom
        ImplementSubjectLifeCycleJudgeComRespDto implementSubjectLifeCycleJudgeComRespDto = null;
        if (queryAlbumFileRelationDetailComRespDto != null) {
            ImplementSubjectLifeCycleJudgeComReqDto implementSubjectLifeCycleJudgeComReqDto = new ImplementSubjectLifeCycleJudgeComReqDto();
            implementSubjectLifeCycleJudgeComReqDto.setLifecyCycleOperationType("ACHIVE");//sourceId:2028502_1
            if (queryAlbumFileRelationDetailComRespDto != null) {
                implementSubjectLifeCycleJudgeComReqDto.setSubjectLifeCycle(queryAlbumFileRelationDetailComRespDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:2028501_1
                implementSubjectLifeCycleJudgeComReqDto.setIsValid(queryAlbumFileRelationDetailComRespDto.getIsValid());//SimpleFieldAssign//sourceId:2028503_1
            }

            /*D8执行生命周期判断(公共)[10346]   */
            Assert.isNull(implementSubjectLifeCycleJudgeComReqDto.getLifecyCycleOperationType(), "B8存档专辑下文件-D8执行生命周期判断(公共)-生命周期操作类型不能为空", false);
            Assert.isNull(implementSubjectLifeCycleJudgeComReqDto.getSubjectLifeCycle(), "B8存档专辑下文件-D8执行生命周期判断(公共)-主体生命周期不能为空", false);
            Assert.isNull(implementSubjectLifeCycleJudgeComReqDto.getIsValid(), "B8存档专辑下文件-D8执行生命周期判断(公共)-是否作废不能为空", false);
            implementSubjectLifeCycleJudgeComRespDto = fwDataassetsUnstructuredResourcesClient.implementSubjectLifeCycleJudgeCom(implementSubjectLifeCycleJudgeComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        }

//步骤2: D8修改专辑文件关系(公共) - updateAlbumFileRelationCom
        UpdateAlbumFileRelationComRespDto updateAlbumFileRelationComRespDto = null;
        UpdateAlbumFileRelationComReqDto updateAlbumFileRelationComReqDto = new UpdateAlbumFileRelationComReqDto();
        updateAlbumFileRelationComReqDto.setIsArchive("TRUE");//sourceId:2028531_1
        updateAlbumFileRelationComReqDto.setSubjectLifeCycle("ARCHIVING");//sourceId:2028532_1
        if (reqDto != null) {
            updateAlbumFileRelationComReqDto.setAlbumRelationId(reqDto.getAlbumRelationId());//SimpleFieldAssign//sourceId:2028530_1
        }

        /*D8修改专辑文件关系(公共)[10325]   */
        Assert.isNull(updateAlbumFileRelationComReqDto.getAlbumRelationId(), "B8存档专辑下文件-D8修改专辑文件关系(公共)-专辑文档关系ID不能为空", false);
        Assert.isNull(updateAlbumFileRelationComReqDto.getIsArchive(), "B8存档专辑下文件-D8修改专辑文件关系(公共)-是否存档不能为空", false);
        Assert.isNull(updateAlbumFileRelationComReqDto.getSubjectLifeCycle(), "B8存档专辑下文件-D8修改专辑文件关系(公共)-主体生命周期不能为空", false);
        updateAlbumFileRelationComRespDto = fwDataassetsAlbumClient.updateAlbumFileRelationCom(updateAlbumFileRelationComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        BffUpdateAlbumFileArchiveRespDto retData = new BffUpdateAlbumFileArchiveRespDto();


        return retData;
    }

    /**
     * B8新增资源文件[10396]
     * gen by moon at 11/28/2024, 12:28:49 AM
     */
    @Trace(operationName = "B8新增资源文件")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffAddResourceFileRespDto addResourceFile(BffAddResourceFileReqDto reqDto) {


        //步骤0: D8循环增资源文件 - loopAddResourceFile
        LoopAddResourceFileRespDto loopAddResourceFileRespDto = null;
        LoopAddResourceFileReqDto loopAddResourceFileReqDto = new LoopAddResourceFileReqDto();
        if (reqDto != null && reqDto.getResourceFileList() != null && !CollectionUtil.isEmpty(reqDto.getResourceFileList())) {
            loopAddResourceFileReqDto.setResourceFileList(reqDto.getResourceFileList().stream().map(item -> BeanUtil.toBean(item, ResourceFileDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:2029012_1
        }

        /*D8循环增资源文件[10395]   */

        loopAddResourceFileRespDto = fwDataassetsResourceFileClient.loopAddResourceFile(loopAddResourceFileReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        BffAddResourceFileRespDto retData = new BffAddResourceFileRespDto();


        return retData;
    }

    /**
     * B8新增资源文件上区块链[10437]
     * gen by moon at 12/12/2024, 5:04:28 PM
     */
    @Trace(operationName = "B8新增资源文件上区块链")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffAddEviFilesChainRespDto addEviFilesChain(BffAddEviFilesChainReqDto reqDto) {


        ImplementEviFilesChainsComRespDto implementEviFilesChainsComRespDto_1 = null;
//步骤0: D8执行存证操作权限判断(公共) - implementEviRecordOperatePrivacyCom
        ImplementEviRecordOperatePrivacyComRespDto implementEviRecordOperatePrivacyComRespDto = null;
        ImplementEviRecordOperatePrivacyComReqDto implementEviRecordOperatePrivacyComReqDto = new ImplementEviRecordOperatePrivacyComReqDto();
        if (reqDto != null) {
            implementEviRecordOperatePrivacyComReqDto.setResourceFileId(reqDto.getResourceFileId());//SimpleFieldAssign//sourceId:2036890_1
            implementEviRecordOperatePrivacyComReqDto.setResourceCopyRightType(reqDto.getResourceCopyRightType());//SimpleFieldAssign//sourceId:2036888_1
            implementEviRecordOperatePrivacyComReqDto.setResourceCopyRightHolderId(reqDto.getResourceCopyRightHolderId());//SimpleFieldAssign//sourceId:2036889_1
        }

        /*D8执行存证操作权限判断(公共)[10452]   */
        Assert.isNull(implementEviRecordOperatePrivacyComReqDto.getResourceFileId(), "B8新增资源文件上区块链-D8执行存证操作权限判断(公共)-资源文件ID不能为空", false);
        Assert.isNull(implementEviRecordOperatePrivacyComReqDto.getResourceCopyRightType(), "B8新增资源文件上区块链-D8执行存证操作权限判断(公共)-资源文件版权归属类型不能为空", false);
        implementEviRecordOperatePrivacyComRespDto = fwDataassetsResourceFileClient.implementEviRecordOperatePrivacyCom(implementEviRecordOperatePrivacyComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


//步骤1: D8执行资源文件上区块链(公共) - implementEviFilesChainsCom
        ImplementEviFilesChainsComRespDto implementEviFilesChainsComRespDto = null;
        ImplementEviFilesChainsComReqDto implementEviFilesChainsComReqDto = new ImplementEviFilesChainsComReqDto();
        if (reqDto != null) {
            implementEviFilesChainsComReqDto.setResourceFileId(reqDto.getResourceFileId());//SimpleFieldAssign//sourceId:2034821_1
        }

        /*D8执行资源文件上区块链(公共)[10434]   */
        Assert.isNull(implementEviFilesChainsComReqDto.getResourceFileId(), "B8新增资源文件上区块链-D8执行资源文件上区块链(公共)-资源文件ID不能为空", false);
        implementEviFilesChainsComRespDto = fwDataassetsResourceFileClient.implementEviFilesChainsCom(implementEviFilesChainsComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        implementEviFilesChainsComRespDto_1 = implementEviFilesChainsComRespDto;

        BffAddEviFilesChainRespDto retData = new BffAddEviFilesChainRespDto();
        if (implementEviFilesChainsComRespDto_1 != null) {
            retData.setDataId(implementEviFilesChainsComRespDto_1.getDataId());//SimpleFieldAssign//sourceId:2034829_1
            retData.setDataHash(implementEviFilesChainsComRespDto_1.getDataHash());//SimpleFieldAssign//sourceId:2034830_1
        }


        return retData;
    }

    /**
     * B8查询存证记录信息[10438]
     * gen by moon at 12/12/2024, 5:04:31 PM
     */
    @Trace(operationName = "B8查询存证记录信息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryEviRecordDetailRespDto queryEviRecordDetail(BffQueryEviRecordDetailReqDto reqDto) {


        QueryEviRecordDetailComRespDto queryEviRecordDetailComRespDto_1 = null;
//步骤0: D8查询存证记录详情(公共) - queryEviRecordDetailCom
        QueryEviRecordDetailComRespDto queryEviRecordDetailComRespDto = null;
        QueryEviRecordDetailComReqDto queryEviRecordDetailComReqDto = new QueryEviRecordDetailComReqDto();
        if (reqDto != null) {
            queryEviRecordDetailComReqDto.setEviRecordId(reqDto.getEviRecordId());//SimpleFieldAssign//sourceId:2034871_1
        }

        /*D8查询存证记录信息[10320]   */
        Assert.isNull(queryEviRecordDetailComReqDto.getEviRecordId(), "B8查询存证记录信息-D8查询存证记录信息-存证记录ID不能为空", false);
        queryEviRecordDetailComRespDto = fwDataassetsResourceFileClient.queryEviRecordDetailCom(queryEviRecordDetailComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        queryEviRecordDetailComRespDto_1 = queryEviRecordDetailComRespDto;

        BffQueryEviRecordDetailRespDto retData = new BffQueryEviRecordDetailRespDto();
        if (queryEviRecordDetailComRespDto_1 != null) {
            retData.setEviRecordId(queryEviRecordDetailComRespDto_1.getEviRecordId());//SimpleFieldAssign//sourceId:2034885_1
            retData.setChainEviId(queryEviRecordDetailComRespDto_1.getChainEviId());//SimpleFieldAssign//sourceId:2034886_1
            retData.setBlockUniCode(queryEviRecordDetailComRespDto_1.getBlockUniCode());//SimpleFieldAssign//sourceId:2034887_1
            retData.setHashCode(queryEviRecordDetailComRespDto_1.getHashCode());//SimpleFieldAssign//sourceId:2034888_1
            retData.setChainAddress(queryEviRecordDetailComRespDto_1.getChainAddress());//SimpleFieldAssign//sourceId:2036673_1
            retData.setHashCal(queryEviRecordDetailComRespDto_1.getHashCal());//SimpleFieldAssign//sourceId:2036674_1
            retData.setResourceFileName(queryEviRecordDetailComRespDto_1.getResourceFileName());//SimpleFieldAssign//sourceId:2034889_1
            retData.setResourceFileNumber(queryEviRecordDetailComRespDto_1.getResourceFileNumber());//SimpleFieldAssign//sourceId:2034890_1
            retData.setPriAuthorName(queryEviRecordDetailComRespDto_1.getPriAuthorName());//SimpleFieldAssign//sourceId:2034891_1
            retData.setCopyRightType(queryEviRecordDetailComRespDto_1.getCopyRightType());//SimpleFieldAssign//sourceId:2034892_1
            retData.setCopyRightHolderName(queryEviRecordDetailComRespDto_1.getCopyRightHolderName());//SimpleFieldAssign//sourceId:2034893_1
            retData.setAbbreviation(queryEviRecordDetailComRespDto_1.getAbbreviation());//SimpleFieldAssign//sourceId:2034894_1
            retData.setCreateInductionId(queryEviRecordDetailComRespDto_1.getCreateInductionId());//SimpleFieldAssign//sourceId:2034895_1
            retData.setCreateTime(queryEviRecordDetailComRespDto_1.getCreateTime());//SimpleFieldAssign//sourceId:2034896_1
        }


        return retData;
    }

    /**
     * B8生成文件简介[10449]
     * gen by moon at 12/13/2024, 11:24:38 PM
     */
    @Trace(operationName = "B8生成文件简介")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQuestionResourceFileDetailRespDto questionResourceFileDetail(BffQuestionResourceFileDetailReqDto reqDto) {


        ImplementFileQaRespDto implementFileQaRespDto_1 = null;
//步骤0: D8查询资源文件详情(公共) - queryResourceFileDetailCom
        QueryResourceFileDetailComRespDto queryResourceFileDetailComRespDto = null;
        QueryResourceFileDetailComReqDto queryResourceFileDetailComReqDto = new QueryResourceFileDetailComReqDto();
        if (reqDto != null) {
            queryResourceFileDetailComReqDto.setResourceFileId(reqDto.getResourceFileId());//SimpleFieldAssign//sourceId:2036916_1
        }

        /*D8查询资源文件详情(公共)[10321]   */
        Assert.isNull(queryResourceFileDetailComReqDto.getResourceFileId(), "B8生成文件简介-D8查询资源文件详情(公共)-资源文件ID不能为空", false);
        queryResourceFileDetailComRespDto = fwDataassetsResourceFileClient.queryResourceFileDetailCom(queryResourceFileDetailComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


//步骤1: D8执行单个文件长文本问答 - implementFileQa
        ImplementFileQaRespDto implementFileQaRespDto = null;
        if (queryResourceFileDetailComRespDto != null) {
            ImplementFileQaReqDto implementFileQaReqDto = new ImplementFileQaReqDto();
            if (reqDto != null) {
                implementFileQaReqDto.setResourceFileId(reqDto.getResourceFileId());//SimpleFieldAssign//sourceId:2035668_1
            }
            if (queryResourceFileDetailComRespDto != null) {
                implementFileQaReqDto.setResourceFileRelaPath(queryResourceFileDetailComRespDto.getResourceFileRelaPath());//SimpleFieldAssign//sourceId:2036661_1
            }

            /*D8执行单个文件长文本问答[10429]   */
            Assert.isNull(implementFileQaReqDto.getResourceFileId(), "B8生成文件简介-D8执行单个文件长文本问答-资源文件ID不能为空", false);
            Assert.isNull(implementFileQaReqDto.getResourceFileRelaPath(), "B8生成文件简介-D8执行单个文件长文本问答-资源文件相对路径不能为空", false);
            implementFileQaRespDto = fwDataassetsIntelligentCommunicationClient.implementFileQa(implementFileQaReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


            implementFileQaRespDto_1 = implementFileQaRespDto;
        }

        BffQuestionResourceFileDetailRespDto retData = new BffQuestionResourceFileDetailRespDto();
        if (implementFileQaRespDto_1 != null) {
            retData.setContent(implementFileQaRespDto_1.getContent());//SimpleFieldAssign//sourceId:2035672_1
        }


        return retData;
    }

    /**
     * B8查询资源文件列表[10529]
     * gen by moon at 6/3/2025, 1:33:52 PM
     */
    @Trace(operationName = "B8查询资源文件列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryResourceFileListRespDto queryResourceFileList(BffQueryResourceFileListReqDto reqDto) {


        QueryResourceFileListComRespDto queryResourceFileListComRespDto_1 = null;
//步骤0: D8查询资源文件列表(公共) - queryResourceFileListCom
        QueryResourceFileListComRespDto queryResourceFileListComRespDto = null;
        QueryResourceFileListComReqDto queryResourceFileListComReqDto = new QueryResourceFileListComReqDto();
        if (reqDto != null) {
            queryResourceFileListComReqDto.setResourceFileName(reqDto.getResourceFileName());//SimpleFieldAssign//sourceId:2047151_1
            queryResourceFileListComReqDto.setResourceCopyRightType(reqDto.getResourceCopyRightType());//SimpleFieldAssign//sourceId:2047153_1
            queryResourceFileListComReqDto.setResourceCopyRightHolderId(reqDto.getResourceCopyRightHolderId());//SimpleFieldAssign//sourceId:2047154_1
            queryResourceFileListComReqDto.setCopyRightOriginalRoleMemberId(reqDto.getCopyRightOriginalRoleMemberId());//SimpleFieldAssign//sourceId:2047155_1
            queryResourceFileListComReqDto.setIsValid(reqDto.getIsValid());//SimpleFieldAssign//sourceId:2047557_1
        }

        /*D8查询资源文件列表(公共)[10393]   */
        Assert.isNull(queryResourceFileListComReqDto.getResourceCopyRightType(), "B8查询资源文件列表-D8查询资源文件列表(公共)-资源文件版权归属类型不能为空", false);
        Assert.isNull(queryResourceFileListComReqDto.getIsValid(), "B8查询资源文件列表-D8查询资源文件列表(公共)-是否作废不能为空", false);
        queryResourceFileListComRespDto = fwDataassetsResourceFileClient.queryResourceFileListCom(queryResourceFileListComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        queryResourceFileListComRespDto_1 = queryResourceFileListComRespDto;

        BffQueryResourceFileListRespDto retData = new BffQueryResourceFileListRespDto();
        if (queryResourceFileListComRespDto_1 != null) {
            retData.setResourceFileList(//objList-to-objLists
                    queryResourceFileListComRespDto_1.getResourceFileList().stream().map(item -> {
                        BffResourceFileDto elm = new BffResourceFileDto();
                        if (item != null) {
                            elm.setResourceFileId(item.getResourceFileId());//SimpleFieldAssign//sourceId:434554_2
                            elm.setResourceFileName(item.getResourceFileName());//SimpleFieldAssign//sourceId:434555_2
                            elm.setResourceFileFullPath(item.getResourceFileRelaPath());//SimpleFieldAssign//sourceId:434556_2
                            elm.setResourceFileSize(item.getResourceFileSize());//SimpleFieldAssign//sourceId:434557_2
                            elm.setResourceFileExtension(item.getResourceFileExtension());//SimpleFieldAssign//sourceId:434558_2
                            elm.setResourceCopyRightHolderId(item.getResourceCopyRightHolderId());//SimpleFieldAssign//sourceId:434559_2
                            elm.setCopyRightOriginalRoleMemberId(item.getCopyRightOriginalRoleMemberId());//SimpleFieldAssign//sourceId:434564_2
                        }
                        return elm;
                    }).collect(Collectors.toList()));//objList-to-objLists//sourceId:2047162_1
        }


        retData.setPageNum(queryResourceFileListComRespDto.getPageNum());
        retData.setPageSize(queryResourceFileListComRespDto.getPageSize());
        retData.setTotal(queryResourceFileListComRespDto.getTotalNum());
        retData.setIsLastPage((Math.ceil(Double.valueOf(queryResourceFileListComRespDto.getTotalNum()) / Double.valueOf(queryResourceFileListComRespDto.getPageSize()))) <= retData.getPageNum());


        return retData;
    }
    //
}
